package com.ruoyi.web.controller.iot;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
@RestController
@RequestMapping("/exclude/api")
public class FileController {

//    private static final String BASE_DIR = "D:\\uploadPath\\";


    @PostMapping("/uploadZip")
    public ResponseEntity<String> uploadZip(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty() || !file.getOriginalFilename().endsWith(".zip")) {
            return ResponseEntity.badRequest().body("只允许上传 ZIP 文件");
        }

        File zipFile = new File(RuoYiConfig.getDownloadPath() + file.getOriginalFilename());
        file.transferTo(zipFile);

        // 解压 ZIP 文件
        unzipFile(zipFile, new File(RuoYiConfig.getDownloadPath()));

        zipFile.delete();

        return ResponseEntity.ok("ZIP 文件上传并解压成功");
    }

    @GetMapping("/files")
    public List<Map<String, String>> listFiles() {
        File folder = new File(RuoYiConfig.getDownloadPath());
        List<Map<String, String>> fileList = new ArrayList<>();

        for (File file : folder.listFiles()) {
            if (file.isFile()) {
                Map<String, String> fileInfo = new HashMap<>();
                fileInfo.put("fileName", file.getName());
                fileInfo.put("filePath", file.getAbsolutePath());
                fileList.add(fileInfo);
            }
        }
        return fileList;
    }

    @PostMapping("/uploadFile")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        File targetFile = new File(RuoYiConfig.getDownloadPath() + file.getOriginalFilename());
        file.transferTo(targetFile);
        return ResponseEntity.ok("文件上传成功");
    }

    @DeleteMapping("/deleteFile/{fileName}")
    public ResponseEntity<String> deleteFile(@PathVariable String fileName) {
        File file = new File(RuoYiConfig.getDownloadPath() + fileName);
        if (file.exists() && file.delete()) {
            return ResponseEntity.ok("文件删除成功");
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("文件不存在");
    }

    public static void unzipFile(File zipFile, File targetDir) throws IOException {
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        try (ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"))) { // 兼容中文文件名
            Enumeration<? extends ZipEntry> entries = zip.entries();

            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File newFile = new File(targetDir, entry.getName());

                if (entry.isDirectory()) {
                    newFile.mkdirs();
                } else {
                    try (InputStream is = zip.getInputStream(entry);
                         FileOutputStream fos = new FileOutputStream(newFile)) {
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = is.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }
                    }
                }
            }
        }
        zipFile.delete(); // 解压后删除 ZIP
    }
}
